package com.qch.rocket.demo.consumer;

import org.apache.rocketmq.client.consumer.DefaultMQPullConsumer;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.PullResult;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Set;

/**
 * @Author qch
 * @Date 2021/6/7
 * 拉取消息的消费者
 */
public class MyConsumer {
    public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, MQBrokerException, UnsupportedEncodingException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consuer_grp_06_02");
        consumer.setNamesrvAddr("192.168.142.134:9876");
      consumer.subscribe("tp_demo_06","tag-1");
      consumer.setMessageListener(new MessageListenerConcurrently() {
          public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
              MessageQueue messageQueue = consumeConcurrentlyContext.getMessageQueue();
              System.out.println("broker"+messageQueue.getBrokerName());
              System.out.println("topic"+messageQueue.getTopic());
              System.out.println("queuedId"+messageQueue.getQueueId());
              for (MessageExt msg : msgs) {
                  System.out.println(msg);
              }
              return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
          }
      });
      consumer.start();
        //consumer.shutdown();
    }
}
